using System;
using Volo.Abp.Domain.Entities.Auditing;

namespace BW.WMS.WareHouseManage
{
    /// <summary>
    /// 库存实体
    /// </summary>
    public class Inventory : FullAuditedEntity<Guid>
    {
        /// <summary>
        /// 物料ID
        /// </summary>
        public Guid MaterialId { get; set; }
        
        /// <summary>
        /// 仓库ID
        /// </summary>
        public Guid WarehouseId { get; set; }
        
        /// <summary>
        /// 库区ID
        /// </summary>
        public Guid ZoneId { get; set; }
        
        /// <summary>
        /// 库位ID
        /// </summary>
        public Guid LocationId { get; set; }
        
        /// <summary>
        /// 批次号
        /// </summary>
        public string BatchNo { get; set; }
        
        /// <summary>
        /// 数量
        /// </summary>
        public decimal Quantity { get; set; }
        
        /// <summary>
        /// 锁定数量（被分配但未实际出库的数量）
        /// </summary>
        public decimal LockedQuantity { get; set; }
        
        /// <summary>
        /// 可用数量（总数量-锁定数量）
        /// </summary>
        public decimal AvailableQuantity => Quantity - LockedQuantity;
        
        /// <summary>
        /// 生产日期
        /// </summary>
        public DateTime? ProductionDate { get; set; }
        
        /// <summary>
        /// 过期日期
        /// </summary>
        public DateTime? ExpiryDate { get; set; }
        
        /// <summary>
        /// 入库日期
        /// </summary>
        public DateTime InboundDate { get; set; }
        
        /// <summary>
        /// 状态 (0-正常，1-待检，2-冻结，3-不良)
        /// </summary>
        public int Status { get; set; }
        
        /// <summary>
        /// 所有者ID（适用于多租户或寄售业务）
        /// </summary>
        public Guid? OwnerId { get; set; }
        
        /// <summary>
        /// 供应商ID（可选）
        /// </summary>
        public Guid? SupplierId { get; set; }
        
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
        
        // 导航属性
        public virtual Material Material { get; protected set; }
        public virtual Warehouse Warehouse { get; protected set; }
        public virtual Zone Zone { get; protected set; }
        public virtual Location Location { get; protected set; }
        
        protected Inventory()
        {
        }
        
        public Inventory(
            Guid id,
            Guid materialId,
            Guid warehouseId,
            Guid zoneId,
            Guid locationId,
            decimal quantity,
            string batchNo = null,
            int status = 0) : base(id)
        {
            MaterialId = materialId;
            WarehouseId = warehouseId;
            ZoneId = zoneId;
            LocationId = locationId;
            Quantity = quantity;
            BatchNo = batchNo;
            Status = status;
            InboundDate = DateTime.Now;
        }
        
        /// <summary>
        /// 增加库存数量
        /// </summary>
        public void IncreaseQuantity(decimal amount)
        {
            if (amount <= 0)
            {
                throw new ArgumentException("增加的数量必须大于0", nameof(amount));
            }
            
            Quantity += amount;
        }
        
        /// <summary>
        /// 减少库存数量
        /// </summary>
        public void DecreaseQuantity(decimal amount)
        {
            if (amount <= 0)
            {
                throw new ArgumentException("减少的数量必须大于0", nameof(amount));
            }
            
            if (amount > AvailableQuantity)
            {
                throw new InvalidOperationException($"可用库存不足，当前可用:{AvailableQuantity}，请求:{amount}");
            }
            
            Quantity -= amount;
        }
        
        /// <summary>
        /// 锁定库存数量
        /// </summary>
        public void LockQuantity(decimal amount)
        {
            if (amount <= 0)
            {
                throw new ArgumentException("锁定的数量必须大于0", nameof(amount));
            }
            
            if (amount > AvailableQuantity)
            {
                throw new InvalidOperationException($"可用库存不足，当前可用:{AvailableQuantity}，请求:{amount}");
            }
            
            LockedQuantity += amount;
        }
        
        /// <summary>
        /// 解锁库存数量
        /// </summary>
        public void UnlockQuantity(decimal amount)
        {
            if (amount <= 0)
            {
                throw new ArgumentException("解锁的数量必须大于0", nameof(amount));
            }
            
            if (amount > LockedQuantity)
            {
                throw new InvalidOperationException($"锁定库存不足，当前锁定:{LockedQuantity}，请求解锁:{amount}");
            }
            
            LockedQuantity -= amount;
        }
    }
} 